class Solution {
    public String convert(String s, int numRows) {

        if(numRows==1) return s;
        char[] ss=s.toCharArray();
        int n=ss.length;
        StringBuffer ret=new StringBuffer();
        int d=2*numRows-2;


        for(int x=0;x<n;x+=d){
            ret.append(ss[x]);
        }


        for(int i=1;i<numRows-1;i++){

            for(int j=i,k=d-i;j<n;j+=d,k+=d){
                ret.append(ss[j]);
                if(k<n) ret.append(ss[k]);
            }

        }


        for(int x=numRows-1;x<n;x+=d){
            ret.append(ss[x]);
        }

        return ret.toString();
    }
}



class Solution1 {
    public String countAndSay(int n) {
        int left=0,right=0;
        String prev="1";

        for(int i=1;i<n;i++){
            char[] s=prev.toCharArray();
            StringBuffer ret=new StringBuffer();
            for(left=0,right=0;right<s.length;right++){
                if(s[right]!=s[left]){
                    int count=right-left;
                    ret.append(count);
                    ret.append(s[left]);
                    left=right;
                }


            }
            int count=right-left;
            ret.append(count);
            ret.append(s[left]);

            prev=ret.toString();
        }

        return prev;
    }
}